<cfcomponent output="false">

	<!--- --->

	<cffunction name="getWorkweekEvents" access="public" output="true" returntype="query">

		<cfreturn application.g.workweekGateway.getWorkweekEvents(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getWorkweekEventsByWorkdays" access="public" output="true" returntype="struct">
		<cfargument name="workdays" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.events = $.run.it('workweekService.getWorkweekEvents',{weekdays=arguments.workdays,noFuture=true}) />

		<cfset local.days = {} />
		
		<cfloop query="local.events">
			<cfif not structKEyExists(local.days,local.events.weekday)>
				<cfset local.days[local.events.weekday] = {} />
			</cfif>
			<!--- set the key for hour --->
			<!--- set the key for minute --->
			<cfif local.events.start_time eq "">
				<cfset local.hourKey = "unscheduled" />
				<cfset local.minuteKey = local.events.id />
			<cfelse>
				<cfset local.hourKey = hour(local.events.start_time) />
				<cfset local.minuteKey = minute(local.events.start_time) />
			</cfif>
			<cfif not structKeyExists(local.days[local.events.weekday],local.hourKey)>
				<cfset local.days[local.events.weekday][local.hourKey] = {} />
			</cfif>
			<cfif not structKeyExists(local.days[local.events.weekday][local.hourKey],local.minuteKey)>
				<Cfset local.days[local.events.weekday][local.hourKey][local.minuteKey] = {} />
			</cfif>
			
			<!--- setup the struct differently for unscheduled --->
			<cfif local.hourKey eq "unscheduled">
				<cfset local.days[local.events.weekday][local.hourKey][local.minuteKey] = {
					id=local.events.id,			
					goal_action_id=local.events.goal_action_id,
					name=local.events.name,
					start_time=local.events.start_time,
					duration=local.events.duration,
					category=local.events.category,
					goal_id=local.events.goal_id,
					goal_name=local.events.goal_name
				} />
			<cfelse>
				<cfset local.days[local.events.weekday][local.hourKey][local.minuteKey][local.events.id] = {
					id=local.events.id,			
					goal_action_id=local.events.goal_action_id,
					name=local.events.name,
					start_time=local.events.start_time,
					duration=local.events.duration,
					category=local.events.category,
					goal_id=local.events.goal_id,
					goal_name=local.events.goal_name
				} />
			</cfif>
		</cfloop>

		<cfreturn local.days />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="saveWorkweekEvent" access="public" output="false" returntype="string">
		<cfargument name="workweekEventID" required="false" default="" />
		<cfargument name="name" required="false" />
		<cfargument name="duration" required="false" />
		<cfargument name="weekday" required="false" />
		<cfargument name="goalActionID" required="false" />
		<cfargument name="start_time" required="false" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		
		<cfset var local = {} />

		<cfset local.args = {
			table="workweek_event",
			id=arguments.workweekEventID,
			user_id=arguments.userID
		} />
		
		<cfif structKeyExists(arguments,"name")>
			<cfset local.args.name = arguments.name />
		</cfif>
		<cfif structKeyExists(arguments,"duration")>
			<cfset local.args.duration = arguments.duration />
		</cfif>
		<cfif structKeyExists(arguments,"weekday")>
			<cfset local.args.weekday = arguments.weekday />
		</cfif>
		<cfif structKeyExists(arguments,'goalActionID')>
			<cfset local.args.goal_action_id = arguments.goalActionID />
		</cfif>
		<cfif structKeyExists(arguments,'start_time')>
			<cfif arguments.start_time neq "">
				<cfset arguments.start_time = dateTimeFormat(arguments.start_time,'yyyy-mm-dd HH:nn:ss') />
			</cfif>
			<cfset local.args.start_time = arguments.start_time />
		</cfif>
		<cfif structKeyExists(arguments,'category')>
			<cfset local.args.category = arguments.category />
		</cfif>

		<cfset local.workweekEventID = $.database.save(argumentCollection=local.args) />
		
		<cfreturn local.workweekEventID />
		
	</cffunction>

	<!--- --->

	<cffunction name="deleteWorkweekEvent" access="public" output="false" returntype="string">
		<cfargument name="workweekEventID" required="true" />
		
		<cfset var local = {} />

		<cfset $.database.delete(
				table="workweek_event",
				id=arguments.workweekEventID
			) />
		
		<cfreturn true />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="humanizeDuration" access="public" output="true" returntype="string">
		<cfargument name="duration" required="true" hint="minutes" />
		
		<cfset var local = {} />
		
		<cfset local.totalMinutes = arguments.duration />
		<cfset local.hours = 0 />
		<cfset local.minutes = local.totalMinutes mod 60 />
		<cfset local.totalMinutes -= local.minutes />
		<cfif local.totalMinutes gt 0>
			<cfset local.hours = local.totalMinutes / 60 />
		</cfif>
		
		<cfset local.result = "" />
		<cfif arguments.duration eq 60>
			<cfset local.result = "1 hr" />
		<cfelseif arguments.duration eq 90>
			<cfset local.result = "90 mins" />
		<cfelseif arguments.duration lt 60>
			<cfset local.result = arguments.duration & " mins" />
		<cfelse>
			<cfif local.minutes eq 0><cfset local.minutes = "00"></cfif>
			<cfset local.result = local.hours & ":" & local.minutes & " hr" />
			<cfif arguments.duration gt 60>
				<cfset local.result = local.result & "s" />
			</cfif>
		</cfif>
		
		<cfset local.result = rereplace(local.result,":00","","ALL") />
				
		<cfreturn local.result />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="bulkDeleteWorkweekEvents" access="public" output="true" returntype="void">

		<cfreturn application.g.workweekGateway.bulkDeleteWorkweekEvents(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>